﻿using FM.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FM.Service
{
    public class ExpenseService : IExpenseService
    {
        private readonly IRepository<Expense> expenseRepository;
        private readonly IUnitOfWork unitOfWork;
        public ExpenseService(IRepository<Expense> expenseRepository, IUnitOfWork unitOfWork)
        {
            this.expenseRepository = expenseRepository;
            this.unitOfWork = unitOfWork;
        }

        public Expense GetExpense(int id)
        {
            var expense = expenseRepository.GetById(id);
            return expense;
        }

        public IEnumerable<Expense> GetExpenses(DateTime startDate, DateTime endDate)
        {
            var expenses = expenseRepository.GetMany(exp => exp.Date >= startDate && exp.Date <= endDate);
            return expenses;
        }

        public void CreateExpense(Expense expense)
        {
            expenseRepository.Add(expense);
            Save();
        }

        public void UpdateExpense(Expense expense)
        {
            expenseRepository.Update(expense);
            Save();
        }

        public void DeleteExpense(int id)
        {
            var expense = expenseRepository.GetById(id);
            expenseRepository.Delete(expense);
            Save();
        }

        private void Save()
        {
            unitOfWork.Commit();
        }
    }
}
